package com.lsl.endworkvol.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.lsl.endworkvol.entity.Admin;
import com.lsl.endworkvol.entity.Leader;
import com.lsl.endworkvol.entity.Volunteer;
import com.lsl.endworkvol.service.*;

@Controller
public class LoginController {

    @Autowired
    private AdminService adminService;

    @Autowired
    private VolunteerService volunteerService;

    @Autowired
    private LeaderService leaderService;

    @RequestMapping(value = { "/", "/system_login_page" }, method = RequestMethod.GET)
    public ModelAndView toLoginPage(ModelAndView modelAndView){
        System.out.println("[Login] +++++ now is login page");
        modelAndView.setViewName("login");
        return modelAndView;
    }

    @RequestMapping(path = "/admin/index", method = RequestMethod.GET)
    public ModelAndView toAdminIndexPage(ModelAndView modelAndView){
        System.out.println("[Login] +++++ now is index page");
        modelAndView.setViewName("admin/index");
        return modelAndView;
    }

    @RequestMapping(path = "/volunteer/index", method = RequestMethod.GET)
    public ModelAndView toVolunteerIndexPage(ModelAndView modelAndView){
        System.out.println("[Login] +++++ now is index page");
        modelAndView.setViewName("volunteer/index");
        return modelAndView;
    }

    @RequestMapping(path = "/leader/index", method = RequestMethod.GET)
    public ModelAndView toleaderIndexPage(ModelAndView modelAndView){
        System.out.println("[Login] +++++ now is index page");
        modelAndView.setViewName("leader/index");
        return modelAndView;
    }

    @RequestMapping(path = "/login", method = RequestMethod.POST)
    public String login(
        @RequestParam(value = "userid") String id, 
        @RequestParam(value = "password") String password,
        HttpServletRequest request
    ) {
        String idHead = id.substring(0, 3);
        String idTail = id.substring(3);
        if(idHead.equals("100")){ // admin
            System.out.println("[Login] +++++ admin login");
            System.out.println("[Login] +++++ id: " + idTail + " password: " + password);
            if(adminService.loginCheck(idTail, password)){
                request.getSession().setAttribute("role", "admin");
                request.getSession().setAttribute("userid", idTail);
                request.getSession().setAttribute("username", ((Admin)adminService.getUser(Integer.parseInt(idTail))).getUsername());
                return "redirect:/admin/index";
            }
            return "redirect:/";
        } else if (idHead.equals("200")){ // volunteer
            System.out.println("[Login] +++++ volunteer login");
            System.out.println("[Login] +++++ id: " + idTail + " password: " + password);
            if(volunteerService.loginCheck(idTail, password)){
                request.getSession().setAttribute("role", "volunteer");
                request.getSession().setAttribute("userid", idTail);
                request.getSession().setAttribute("username", ((Volunteer)volunteerService.getUser(Integer.parseInt(idTail))).getName());
                return "redirect:/volunteer/index";
            }
            return "redirect:/";
        } else if (idHead.equals("300")){ // leader
            System.out.println("[Login] +++++ leader login");
            System.out.println("[Login] +++++ id: " + idTail + " password: " + password);
            if (leaderService.loginCheck(idTail, password)) {
                request.getSession().setAttribute("role", "leader");
                request.getSession().setAttribute("userid", idTail);
                request.getSession().setAttribute("username", ((Leader)leaderService.getUser(Integer.parseInt(idTail))).getUsername());
                return "redirect:/leader/index";
            }
            return "redirect:/";
        } else { //unknow
            return "redirect:/";
        }
    }

    @RequestMapping(path = "/logout", method = RequestMethod.GET)
    public void logout(HttpServletRequest request, HttpServletResponse response) {
        String jsCode = "<script type='text/javascript'>top.location.href='system_login_page';</script>";
        try {
            request.getSession().invalidate();
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write(jsCode);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
